package com.git.groovy.utils;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.*;
import org.eclipse.jgit.util.FS;
import org.junit.jupiter.api.Test;

import java.io.File;

public class JgitDemo {

    /**
     * 指定ssh私要目录
     */
    @Test
    public void test1() {
        final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            /**
             *  解除HostKey检查，也就意味着可以接受未知的远程主机的文件，这是不安全的，这种模式只是用于测试为目的的。
             *  利用ssh-keyscan -t rsa hostname，收集主机数据。
             * @param host
             * @param session
             */
            @Override
            protected void configure(OpenSshConfig.Host host, Session session) {
                session.setConfig("StrictHostKeyChecking", "no");
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch jSch = super.createDefaultJSch(fs);
                jSch.addIdentity("C:/Users/xxx/.ssh/id_rsa_git");
                jSch.setKnownHosts("C:/Users/xxx/.ssh/known_hosts");
                return jSch;
            }
        };
        CloneCommand cloneCommand = Git.cloneRepository();
        cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
            @Override
            public void configure(Transport transport) {
                SshTransport sshTransport = (SshTransport) transport;
                sshTransport.setSshSessionFactory(sshSessionFactory);
            }
        });
        cloneCommand.setURI("git@github.com:xxx/xxx.git");
        cloneCommand.setDirectory(new File("C:/Users/xxx/Documents/code/testtest"));
        try {
            cloneCommand.call().checkout();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过私钥String来
     */
    @Test
    public void test2() {
        final SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
            /**
             *  解除HostKey检查，也就意味着可以接受未知的远程主机的文件，这是不安全的，这种模式只是用于测试为目的的。
             *  利用ssh-keyscan -t rsa hostname，收集主机数据。
             * @param host
             * @param session
             */
            @Override
            protected void configure(OpenSshConfig.Host host, Session session) {
                session.setConfig("StrictHostKeyChecking", "no");
            }

            @Override
            protected JSch createDefaultJSch(FS fs) throws JSchException {
                JSch jSch = super.createDefaultJSch(fs);
                String priKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
                        "MIIEogIBAAKCAQEAxXEYGprijLyZYYfjajCYCgbkPtffVxv6FlXhuICeQqQGs5RB\n" +
                        "0H26IiqhpBlH5Rzi+1VSuCkZ+L8bPpboanITTEB5Bl2s6ybBOBGXnhkMcRMh/dM9\n" +
                        "7HY1LykYT4hpE6bCSBFCmX7FIjYnUMsB28EPcEteryf/Dhim1ydhTrJ6+9frKSqV\n" +
                        "0GAJNYJZkJyTKZRwZcR/n0iD4CMKrCkob368MPHhlmhzuexCPJeuhp8uWlYvWctC\n" +
                        "/62oM6+j2M9vZMLMbBn2ok0sz2TmtQ+im4/Ysn14T/tgaXctxGEDqkFoeoM2L60K\n" +
                        "nm2uXLIgGGR3A42sq7UriYpKcGyjk7vR8Rt96QIDAQABAoIBAAF2DcycmtKZzrTH\n" +
                        "EarGg0mHtYa1iESXw2kI56u63/D0T8xia35Ef+0izaFuQnbymMdtYThVrYiGuYfk\n" +
                        "DAJFp9jnnT2r0czHs6RblPipjMsFbjn3pzypv0kDD9f+1XA8yrFH12tVYdYX+oCB\n" +
                        "kBj1O80D8cZwEFYlXo63DuRLM9fLROxjT1wNixQlQysmdpccc/WTcfDdEwjFmIoB\n" +
                        "f1EgtI72XOXZ1legZmBcCI3UqzkFkamGzOS3e9tGee+Ekg/zLdTRC1fkKMuuGUbM\n" +
                        "ZZVtGC13hWSKSb5KuC3uwpsnxaA+WDoKha7e4OvpVMCGVA/dHiLsOe4BaaSDn0Sp\n" +
                        "YibCYNUCgYEA+obG7ohCvFzIX7fRugAsk1x1I089st+edoB0hTKIqB4bEKmVGEVc\n" +
                        "ZofMB9llPP0pGiogQFo9uaa534MhtIblMeTFg1Sqmn92356Imqh8cMDaztsYgVJt\n" +
                        "iGx5TYvp11dPB63CA+O0fxSxgMrxsuTlO1MQCrOGFxR+sMi+xTy60/MCgYEAycFo\n" +
                        "iYgIpSjHAYvsPSWwI8BBXTSWOALTD+T03PhUuf09SjbcwZxJIHOnzc7j4KL1068o\n" +
                        "8PRp0rPYGMqXQTCbvV45ckgXY2tu16Man4gYXZ5lgdYtJYOHBZfOF0jlq4VljMlO\n" +
                        "7sT7zTb6d0NggpEadH3uDCDJy/Eqw6pyU+MTSbMCgYBD3SicSwsQ7W1WCHxmj0+D\n" +
                        "56LPmtKS1uKdJTTrNAIy0FzyiY2a0ocyOwz4/1neSJHOxaSQ6iEYXmKxelsyBOyn\n" +
                        "NFqQtem1ClxQPSiyBHwZKxTavh0IQyTqtL+zUvhYXg1Jfjc1SISHfDqNMeyBUmIK\n" +
                        "TPjmfTUQoXukNez/jvDpIwKBgCD63bTFqOwem1FUZxyjwYRGlCjBROEJDSlm+uP4\n" +
                        "MBlrHQ65oyJ/ArEsVrzc2gFJgjfFRUMGIRpRTbzXuRvt7zdoZKmawT/+OtNDFUII\n" +
                        "xDrhfqRXt2+PbY3rf8PspaaBGtWFvsJnLuYf25eNSwRs5c8vGq+e2H3fAWeUwDWY\n" +
                        "Q/njAoGAZtJyxrb412bOWmKehsPA4xccPNgBsJiehwU2dNJPj69GxGV7zEmTcFTk\n" +
                        "n2wNzs6bT8t3BM1Mzro5B6FEuitO7i2LCDpL4Ytk8474buo/dNrqSRrDrOJ7GCcN\n" +
                        "QD8RPyyYJYOZjQktXGd/O0ZWi+UormH1TmscZiC7ktxjnnrImxQ=\n" +
                        "-----END RSA PRIVATE KEY-----\n";

                jSch.addIdentity("test",priKey.getBytes(),(byte[])null,(byte[])null);
                return jSch;
            }
        };
        CloneCommand cloneCommand = Git.cloneRepository();
        cloneCommand.setTransportConfigCallback(new TransportConfigCallback() {
            @Override
            public void configure(Transport transport) {
                SshTransport sshTransport = (SshTransport) transport;
                sshTransport.setSshSessionFactory(sshSessionFactory);
            }
        });
        cloneCommand.setURI("git@gitee.com:alan_lw/favorite.git");
        cloneCommand.setDirectory(new File("C:\\Users\\Alan\\Desktop\\ttt"));
        try {
            cloneCommand.call().checkout();
        } catch (GitAPIException e) {
            e.printStackTrace();
        }
    }

}
